<?php include("ADLIB.h");
CheckLogin();

error_reporting(0);

//if new directory is requested...
if($_GET['dir']){
//if up requested, remove "folder/../"
$dname=str_replace("/../", '', $_GET['dir']);
$dname=substr($dname, 0, strrpos($dname, '/') + 1);

$expire=time() + (3600*24); //one day.
setcookie('dircook', $dname, $expire); //only reletive path.
# echo Dbug($dname, 'dircookie', __LINE__, 'filelist');

$dname=doc_root() . $dname;
# Dbug($dname, 'dname', __LINE__);
}
// if new dir is not requested...
else{
// if cookie is set...
if($_COOKIE['dircook']){
//if cookie deletion is requested...
if($_GET['data']=='reset'){
//delete it and use root.
setcookie('dircook', '', time()-3600);
$dname=doc_root()."/";
}
//if del not requested, get it.
else{
$dname=$_COOKIE['dircook'];
$dname=doc_root() . $dname;
}
}
// if cookie not set, use root.
else $dname=doc_root()."/";
}

#echo Dbug($dname, 'final dname', __LINE__, 'filelist');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel='shortcut icon' href='/favicon.ico'/>
<link rel="stylesheet" type="text/css" href="style.css"/>
<?php
echo "<title>LIST: ".path2url($dname)."</title>";
echo "</head><body>";

echo "<div class='head'>\n<h3>Directory Listing</h3>\n</div>";

echo '<b>Contents of: <span class="itxt">'.$dname.'</span></b><br/>';
echo "<hr/><a href='#bottom' name='top'>Go Down</a>&nbsp;|&nbsp;<a href='filelist.php?data=reset'>Reset Data</a>";
$dir=opendir($dname);
if(!$dir) exit('<br/><hr/>OPENDIR FAILED!<hr/></body></html>');

$filelist=array();
while(($file=readdir($dir))!==false) $filelist[]=$file;
sort($filelist);
$i=0;

while($filelist[$i]){
//if target is not "."...
if($filelist[$i]!='.'){
//if target is a dir, show only delete & rename...
if(is_dir($dname.$filelist[$i])){
echo "<hr/><b>" .$i. "</b>&#160;&#160;<a href='filelist.php?dir=";
echo path2url($dname).$filelist[$i]. "/'>" .$filelist[$i]. "</a><br/>";
echo '&#160;&#160;&#160;&#160;&#160;';
echo '<a href="others.php?func=d&target=dir&value='.$dname.$filelist[$i].'"><b>D</b></a>&#160;';
echo '<a href="others.php?func=r&value='.$dname.$filelist[$i].'"><b>R</b></a>&#160;';
}
//if target is a file show d, r, v, c, s
else{
echo "<hr/><b>".$i."</b>&#160;&#160;".$filelist[$i]."<br/>";
echo '&#160;&#160;&#160;&#160;&#160;';
echo '<a href="edit.php?fname='.$dname.$filelist[$i].'"><b>V</b></a>&#160;';
echo '<a href="others.php?func=d&value='.$dname.$filelist[$i].'"><b>D</b></a>&#160;';
echo '<a href="others.php?func=r&value='.$dname.$filelist[$i].'"><b>R</b></a>&#160;';
echo '<a href="others.php?func=c&value='.$dname.$filelist[$i].'"><b>C</b></a>&#160;';
echo '<a href="'.path2url($dname.$filelist[$i]).'"><b>S</b></a>';
}
}
$i++;
}
closedir($dir);
?>
<hr/><a href='#top' name='bottom'><u>Goto Top</u></a>  
<br/><form action="filelist.php">
<input type="text" name="dir" value="<?php echo path2url($dname); ?>"/><br/>
<input type="submit" value="View List"/></form><hr/>
<a href='index.php'>cPanel</a>
</body></html>
